package me.zhengjie.modules.utils.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtils {

    /**
     * 根据获取类型 获取两个日期之间所有的日期 如:2022-01-01/ 2022-01/ 2022
     * @param type 1.代表年月日  2.代表年月 3.代表年
     * @param startTime 开始日期
     * @param endTime 结束日期
     * @return
     */
    public static List<String> getBetweenDate(String type, String startTime, String endTime)  {
        SimpleDateFormat sdf = null;
        int calendarType=0;
        if("1".equals(type)){
            sdf = new SimpleDateFormat("yyyy-MM-dd");
            calendarType=Calendar.DATE;
        }else if("2".equals(type)){
            sdf = new SimpleDateFormat("yyyy-MM");
            calendarType=Calendar.MONTH;
        }else if("3".equals(type)){
            sdf = new SimpleDateFormat("yyyy");
            calendarType=Calendar.YEAR;
        }
        List<String> list = new ArrayList<String>();
        try {
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);
            Calendar calendar = Calendar.getInstance();
            while (startDate.getTime()<=endDate.getTime()){
                list.add(sdf.format(startDate));
                calendar.setTime(startDate);
                calendar.add(calendarType,1);
                startDate=calendar.getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return list;
    }


    /**
     * 获取过去六个月的日期+月份
     * @return
     */
    public static List<String> dateArr(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Calendar c = Calendar.getInstance();

        List<String> dateArr=new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            c.setTime(new Date());
            c.add(Calendar.MONTH, -i);
            Date m3 = c.getTime();
            String mon3 = format.format(m3);
            dateArr.add(mon3);
        }
        Collections.reverse(dateArr);

        return dateArr;
    }

    public static List<String> dateArr(int j){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Calendar c = Calendar.getInstance();

        List<String> dateArr=new ArrayList<>();
        for (int i = 0; i < j; i++) {
            c.setTime(new Date());
            c.add(Calendar.MONTH, -i);
            Date m3 = c.getTime();
            String mon3 = format.format(m3);
            dateArr.add(mon3);
        }
//        Collections.reverse(dateArr);
        return dateArr;
    }



    /**
     * 获取几天前到现在的日期，从小到大
     *
     * @param days
     * @param format
     * @return
     */
    public static List<String> getAfewDaysToNowDate(Integer days, String format) {
        List<String> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        list.add(sdf.format(calendar.getTime()));
        for (int i = 0; i < days; i++) {
            calendar.add(Calendar.HOUR, -24);
            list.add(sdf.format(calendar.getTime()));
        }
        Collections.reverse(list);
        return list;
    }

    /**
     * 获取从开始到结束的时间列表
     *
     * @param start
     * @param end
     * @return
     */
    public static List<String> getDateListByStartAndEnd(String start, String end) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<String> date = new ArrayList<>();
        try {
            date.add(start);
            calendar.setTime(simpleDateFormat.parse(start));
            for (int i = 0; ; i++) {
                calendar.add(Calendar.DATE, 1);
                String time = simpleDateFormat.format(calendar.getTime());
                date.add(time);
                if (time.equals(end)) break;
            }
        } catch (ParseException e) {
            e.printStackTrace();
            System.out.println(start + "时间转换出错！");
            return new ArrayList<>();
        }
        return date;
    }



}
